home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Classes / PtrNumberLine / PtrNumberLine.m < prev    next >
Encoding:
Text File  |  1992-06-04  |  4.7 KB  |  199 lines

  1. // By Judy D. Halchin, Educational Computing Services, Allegheny College.
  2. // You may freely copy, distribute and reuse this code. 
  3. // Allegheny College and the author disclaim any warranty of any kind, 
  4. // expressed or implied, as to its fitness for any particular use.
  5. // This work was partially supported by a Teacher Preparation grant from the 
  6. // National Science Foundation.
  7.  
  8. #import "PtrNumberLine.h"
  9. #import "drawPointer.h"
  10. #import <appkit/NXImage.h>
  11. #import <appkit/Application.h>
  12. #import <appkit/nextstd.h>
  13.  
  14. @implementation PtrNumberLine
  15.  
  16. - initFrame:(NXRect *)frameRect
  17. {
  18.     NXSize pointerSize, imageSize;
  19.     float unscaleFactor;
  20.     
  21.     [super initFrame:frameRect];
  22.     
  23.     //initialize all instance variables
  24.       x = 0.25;
  25.       target = nil;
  26.           
  27.     //make an NXImage to hold the pointer and draw the pointer in it
  28.       pointerSize.width = POINTERSIZE;   // must be square for pointer to be
  29.       pointerSize.height = POINTERSIZE;  // centered correctly with the square
  30.       pointerNXImage = [[NXImage alloc] initSize:&pointerSize];
  31.       [pointerNXImage lockFocus];
  32.       drawPointer(POINTERSIZE);
  33.       [pointerNXImage unlockFocus];
  34.  
  35.     //keep track of where the pointer image is, in our coordinates
  36.       [numberLineNXImage getSize:&imageSize];
  37.       unscaleFactor = bounds.size.width 
  38.                                       / MAX(imageSize.width, imageSize.height);
  39.           
  40.       pointerRect.size.width = pointerSize.width * unscaleFactor;
  41.       pointerRect.size.height = pointerSize.height;
  42.       pointerRect.origin.x = x - (pointerSize.width/2 * unscaleFactor);
  43.       pointerRect.origin.y = -pointerSize.height;
  44.  
  45.     return self;
  46. }
  47.  
  48. - drawSelf:(const NXRect *)rects :(int)rectCount
  49. {
  50.     [numberLineNXImage composite:NX_SOVER toPoint:&bounds.origin];
  51.     [pointerNXImage  composite:NX_SOVER toPoint:&pointerRect.origin];
  52.     return self;
  53. }
  54.  
  55. - (float)x
  56. {
  57.     return x;
  58. }
  59.  
  60. - setX:(float)newX
  61. {
  62.     x = newX;
  63.     pointerRect.origin.x = x - pointerRect.size.width/2;
  64.     return self;
  65. }
  66.  
  67. - pointerNXImage
  68. {
  69.     return pointerNXImage;
  70. }
  71.  
  72. - (NXPoint)pointerLocation
  73. {
  74.     return pointerRect.origin;
  75. }
  76.  
  77. - mouseDown:(NXEvent *)theEvent
  78. {
  79.     BOOL mouseWentUp;
  80.     NXEvent *nextEvent;
  81.     NXPoint mousePoint, oldMousePoint;
  82.     NXSize imageSize;
  83.     int oldEventMask;
  84.     float offset, unscaleFactor;
  85.     
  86.     [numberLineNXImage getSize:&imageSize];
  87.     unscaleFactor = bounds.size.width / MAX(imageSize.width, imageSize.height);
  88.  
  89.     mousePoint = theEvent -> location;
  90.     [self convertPoint:&mousePoint fromView:nil];
  91.     if ([self mouse:&mousePoint inRect:&pointerRect])
  92.     {
  93.         oldEventMask = [window addToEventMask:NX_LMOUSEDRAGGEDMASK];
  94.         offset = pointerRect.origin.x - mousePoint.x;
  95.         mouseWentUp = NO;
  96.         while (!mouseWentUp)
  97.         {
  98.                nextEvent = [NXApp getNextEvent:
  99.                                     (NX_LMOUSEUPMASK|NX_LMOUSEDRAGGEDMASK)];
  100.                if (nextEvent->type == NX_LMOUSEUP)
  101.                    mouseWentUp = YES;
  102.                else   
  103.                {
  104.                 //save old and new mouse point
  105.                   oldMousePoint = mousePoint;
  106.                   mousePoint = nextEvent -> location;
  107.                   
  108.                 //calculate new rectangle for where pointer is drawn
  109.                      [self convertPoint:&mousePoint fromView:nil];
  110.                   pointerRect.origin.x = mousePoint.x + offset;
  111.     
  112.                 //calculate new x value from pointer position
  113.                   x = pointerRect.origin.x + pointerRect.size.width/2;              
  114.  
  115.                 //if x is out of view, revert to previous point
  116.                   if ((x < bounds.origin.x) 
  117.                                   || (x > bounds.origin.x + bounds.size.width))
  118.                   {
  119.                         mousePoint = oldMousePoint;  // this has been converted
  120.                         pointerRect.origin.x = mousePoint.x + offset;
  121.                         x = pointerRect.origin.x + pointerRect.size.width/2;              
  122.                   }  
  123.                   
  124.                 //redraw if we're supposed to
  125.                   if (!vFlags.disableAutodisplay)
  126.                         [self display];
  127.  
  128.                 //notify target that x has changed
  129.                   if (target != nil)
  130.                       if ([target respondsTo:action])
  131.                           [target perform:action with:self];
  132.             } 
  133.         }
  134.         [window setEventMask:oldEventMask];        
  135.     }    
  136.     return self;
  137. }
  138.  
  139. - target
  140. {
  141.     return target;
  142. }
  143.  
  144. - (SEL)action
  145. {
  146.     return action;
  147. }
  148.  
  149. - setTarget:anObject
  150. {
  151.     target = anObject;
  152.     return self;
  153. }
  154.  
  155. - setAction:(SEL)anAction
  156. {
  157.     action = anAction;
  158.     return self;
  159. }
  160.  
  161. - setMin:(float)newMin max:(float)newMax
  162. {
  163.     BOOL saveDisplayMode;
  164.     float unscaleFactor;
  165.     NXSize imageSize;
  166.     
  167.  
  168.     saveDisplayMode = vFlags.disableAutodisplay;
  169.     if (!saveDisplayMode)
  170.         [self setAutodisplay:NO];
  171.  
  172.     [super setMin:newMin max:newMax];
  173.  
  174.     [numberLineNXImage getSize:&imageSize];
  175.     unscaleFactor = bounds.size.width / MAX(imageSize.width, imageSize.height);
  176.     pointerRect.size.width = POINTERSIZE * unscaleFactor;
  177.     
  178.     if (x < bounds.origin.x) 
  179.         x = bounds.origin.x;
  180.     if (x > bounds.origin.x + bounds.size.width) 
  181.         x = bounds.origin.x + bounds.size.width;
  182.         
  183.     pointerRect.origin.x = x - pointerRect.size.width/2;
  184.     
  185.     if (!saveDisplayMode)
  186.     {
  187.         [self setAutodisplay:YES];
  188.         [self display];
  189.     }
  190.     return self;
  191. }
  192.  
  193. - (BOOL)acceptsFirstMouse
  194. {
  195.     return YES;
  196. }
  197.  
  198. @end
  199.